package xm.components.button
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import xm.core.XMSprite;
	
	
	/**
	 * <br> 클래스 설명 : </br>
	 * @author 김정환
	 */	
	public class SWCButton extends XMSprite
	{
		
		//-----------------------------------------------------------------------------------
		// public vars
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// protected / internal vars
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// private vars
		//-----------------------------------------------------------------------------------
		
		private var _skin : MovieClip;
		
		private var _labelTF : TextField;
		
		private var _target : Array;
		
		//-----------------------------------------------------------------------------------
		// public functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * Skin 의 MOUSE_DOWN, MOUSE_UP 이벤트에 반응하는 swc 버튼.
		 * @param __skin : MovieClip
		 * @param __target : [MovieClip, MovieClip...]
		 */		
		public function SWCButton( __skin : MovieClip = null, __target : Array = null )
		{
			super();
			
			if( __skin )
			{
				skin = __skin;
			}
			
			if( __target )
			{
				target = __target;
			}
			
		}
		
		
		
		/**
		 * commitProperties
		 */		
		override public function commitProperties():void
		{
			super.commitProperties();
			
		}
		
		
		
		/**
		 * updateDisplayList
		 */		
		override public function updateDisplayList( enforce : Boolean = false ):void
		{
			var wid : Number = 0, hei : Number = 0;
			
			
			if( _labelTF )
			{
				this.setChildIndex( _labelTF, numChildren-1 );
				_labelTF.x = ( this.width - _labelTF.width ) / 2;
				_labelTF.y = ( this.height - _labelTF.height ) / 2;
			}
			else
			{
				
			}
			
			super.updateDisplayList(enforce);
			
		}
		
		
		
		/**
		 * @return MovieClip
		 */		
		public function get skin():MovieClip
		{
			return _skin;
		}
		
		/**
		 * <br>디폴트와 다운이 설정된 2프레임짜리 무비클립을 스킨으로 삽입해야 함.</br>
		 * @param value
		 */		
		public function set skin(value:MovieClip):void
		{
			if( _skin && this.contains( _skin ) )
			{
				this.removeChild( _skin );
				_skin = null;
				_target = null;
			}
			
			_skin = value;
			this.addChild( _skin );
			
			_target = [_skin];
		}
		
		/**
		 * TouchBegin, TouchEnd 이벤트에 반응할 대상 
		 * @param value : Array
		 */		
		public function set target( value : Array ):void
		{
			_target = value;
		}
		
		/**
		 * @param str 레이블 
		 */		
		public function set label( str : String ):void
		{
			
			if( _labelTF == null )
			{
				var tf : TextFormat;
				
				tf = new TextFormat( "돋움", 20, 0x666666, true );
				
				_labelTF = new TextField();
				_labelTF.defaultTextFormat = tf;
				_labelTF.autoSize = TextFieldAutoSize.LEFT;
				_labelTF.selectable = false;
				
				addChild( _labelTF );
			}
			
			_labelTF.text = str;
			_labelTF.width = _labelTF.textWidth + 4;
			_labelTF.height = _labelTF.textHeight + 4;
		}
		
		/**
		 * @return 레이블 텍스트 또는 ""; 
		 */		
		public function get label():String
		{
			return ( _labelTF ) ? _labelTF.text : ""; 
		}
		
		
		/**
		 * <br>레이블 필드 반환</br>
		 * @return 
		 */		
		public function get textField():TextField
		{
			return _labelTF;
		}
		
		//-----------------------------------------------------------------------------------
		// life cycle functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * createChildren
		 */		
		override public function createChildren():void
		{
			super.createChildren();
			
			this.addEventListener( MouseEvent.MOUSE_DOWN, onDown );
			this.addEventListener(MouseEvent.MOUSE_OUT, onUp );
			this.addEventListener( MouseEvent.MOUSE_UP, onUp );
		}
		
		/**
		 * createComplete
		 */		
		override public function creationComplete():void
		{
			super.creationComplete();
			
		}
		
		
		/**
		 * add
		 */		
		override public function add():void
		{
			super.add();
		}
		

		/**
		 * remove
		 */		
		override public function remove():void
		{
			super.remove();
		}
		
		
		/**
		 * destroy
		 */		
		override public function destroy():void
		{
			
			
			if( _skin )
			{
				if( _skin.parent ) _skin.parent.removeChild( _skin );
				
				_skin = null;
			}
			
			if( _labelTF )
			{
				if( _labelTF.parent )  _labelTF.parent.removeChild( _labelTF );
				
				_labelTF = null;
			}
			
			if( _target )
			{
				var i:int, len : int;
				i=0;
				len = _target.length;
				
				while( i<len )
				{
					delete _target[ i ];
					++i;
				}
				
				_target = null;
			}
			
			this.removeEventListener( MouseEvent.MOUSE_DOWN, onDown );
			this.removeEventListener(MouseEvent.MOUSE_OUT, onUp );
			this.removeEventListener( MouseEvent.MOUSE_UP, onUp );
			
			super.destroy();
			
			
		}
		
		//-----------------------------------------------------------------------------------
		// setter / getter
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// protected functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// internal functions
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// private functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// event handler
		//-----------------------------------------------------------------------------------
		
		/**
		 * @param e
		 */		
		protected function onUp(e:MouseEvent):void
		{
			if( skin && _target )
			{
				for each( var mc : MovieClip in _target )
				{
					mc.gotoAndStop( 1 );
				}
			}
		}
		
		/**
		 * @param e
		 */		
		protected function onDown(e:MouseEvent):void
		{
			if( skin && _target )
			{
				for each( var mc : MovieClip in _target )
				{
					mc.gotoAndStop( 2 );
				}
			}
		}		
		
		
	}
}


